package com.geyue.core.service.impl;

import com.geyue.common.base.service.impl.BaseServiceImpl;
import com.geyue.common.toolbox.result.Res;
import com.geyue.core.mapper.BusinessMapper;
import com.geyue.core.mapper.IntegralMapper;
import com.geyue.core.mapper.ProductPropMapper;
import com.geyue.core.mapper.UserMapper;
import com.geyue.core.model.Business;
import com.geyue.core.model.Integral;
import com.geyue.core.model.ProductProp;
import com.geyue.core.model.User;
import com.geyue.core.service.IntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Lyon on 2017/3/27.
 */
@Service
public class IntegralServiceImpl extends BaseServiceImpl<Integral> implements IntegralService {

    @Autowired
    private IntegralMapper integralMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ProductPropMapper propMapper;

    @Override
    public List<Integral> orderByTime(Integer uid) {
        return integralMapper.orderByTime(uid);
    }

    @Transactional
    @Override
    public Res exchange(Integral integral) {

        User user = userMapper.selectByPrimaryKey(integral.getUid());

        Integer integral_ = user.getIntegral();

        if(integral_ < integral.getIntegral())
            return Res.fail("积分不足!");

        User param = new User();
        param.setId(integral.getUid());
        param.setIntegral(integral_ - integral.getIntegral());

        if(userMapper.updateByPrimaryKeySelective(param) > 0){
            if(integralMapper.insertSelective(integral) > 0)
                return Res.success("提交成功!");
        }

        return Res.fail("兑换失败!");
    }

    @Override
    public Res receive(Integer quarter,Integer uid) {

        List<Business> count = businessMapper.count(quarter,uid);
        Integer totalIntegral = 0;
        for (Business item : count) {
            List<ProductProp> props = propMapper.getByPid(Integer.valueOf(item.getProduct()));
            Integer integral = 0;
            Double money = item.getMoney();

            for (ProductProp prop : props) {
                if (money > prop.getMin() && money <= prop.getMax()) {
                    integral = new Double(money * 10000 * prop.getValue() / 100).intValue();
                }
            }

            totalIntegral += integral;
        }

        List<Integer> ids = businessMapper.receive(quarter,uid);

        if(ids.isEmpty())
            return Res.fail("无可领取积分!");

        if(businessMapper.doIsGet(ids) > 0){
            if(userMapper.additionByID(uid,totalIntegral) > 0){
                Integral integral = new Integral();
                integral.setUid(uid);
                integral.setContent("领取"+totalIntegral+"积分");
                integral.setIntegral(totalIntegral);
                if(integralMapper.insertSelective(integral) > 0)
                    return Res.success("领取成功!");
            }
        }


        return Res.fail("领取失败!");
    }
}
